package com.yicong.ants.ui.find;


import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.databinding.NewsWebViewActivityBinding;
import com.yicong.ants.manager.EnergyHelper;
import com.yicong.ants.manager.ad.AdManager;
import com.zx.sdk.util.LeagueMemberHelper;
import com.zx.sdk.util.ZxEventHelper;
import com.zx.sdk.view.widget.BaseNewsAdView;

import java.util.ArrayList;

public class NewsActivity extends BaseTitleBarActivity<NewsWebViewActivityBinding> {
    private BaseNewsAdView newsAdView;

    @Override
    protected int getLayout() {
        return R.layout.news_web_view_activity;
    }

    private boolean clearHistory = false;
    private final ArrayList<String> newsIds = new ArrayList<>();

    @Override
    protected void initEventAndData() {
        setTitleText("新闻资讯任务");
        newsAdView = AdManager.getNewsView(this);
        if (newsAdView == null) {
            ToastUtils.showLong("新闻资讯任务加载错误");
            finish();
            return;
        }
        mDataBind.adViewContainer.addView(newsAdView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mDataBind.adViewTimer.setTimeUpCallback(intValue -> {
            mDataBind.adViewTimer.reset();
            newsAdView.backToHome(true);
        });
        newsAdView.setListener(new BaseNewsAdView.INewsAdViewListener() {
            @Override
            public void onNewsPageStart(String newsId) {
                EnergyHelper.mEnergyType = "1038";
                mDataBind.adViewTimer.reset();
                mDataBind.adViewTimer.startTimeForNews(mDisposable, newsId);
                ToastUtils.showLong("计时开始，20S后您将获得奖励");
                ZxEventHelper.record("news", "click", newsAdView.getAdInfo(), LeagueMemberHelper.getMember(newsAdView.getAdInfo().getLeague()));
            }

            @Override
            public void onBackToHome() {
                mDataBind.adViewTimer.reset();
                if (!mDataBind.adViewTimer.isTimeUp()) {
                    NewsActivity.this.showToast("阅读时长不足20S，无法获得活跃度");
                }
            }
        });
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (event.getCode()) {
            case Constants.Event.Ants_Ad_Complete_For_News:
                String newId = event.getContent();
                if (!newsIds.contains(newId)) {
                    newsIds.add(newId);
                }
                int size = newsIds.size();
                mDataBind.progressBar.setProgress(size);
                mDataBind.progressTxt.setText(size + "/3");
                if (size >= 3) {
                    EnergyHelper.doneEnergyTask(thisActivity);
                } else {
                    UiHelper.showConfirmDialog(thisActivity, "再阅读" + (3 - size) + "篇文章就可以获得奖励了哦", null);
                }
                ZxEventHelper.record("news", "reward", newsAdView.getAdInfo(), LeagueMemberHelper.getMember(newsAdView.getAdInfo().getLeague()));
                break;
        }
    }

    @Override
    protected void onLoadData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (newsAdView != null) {
            newsAdView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (newsAdView != null) {
            newsAdView.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (newsAdView != null) {
            newsAdView.onDestroy();
        }
    }

    @Override
    public void onBackPressed() {
        if (newsAdView != null && newsAdView.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
}
